package com.example.controller;

import com.example.base.ResultVO;
import com.example.modelfunction.shrio.usermanager.mode.User;
import com.example.modelfunction.shrio.usermanager.repository.UserRepository;
import com.example.repository.WeixinRepository;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.example.base.Action.BaseAction;
import com.example.entity.Machine;
import com.example.repository.MachineRepository;

/**
 * Created by Administrator on 2019/5/29.
 */
@RestController
@RequestMapping(value = "machineAction")
public class MachineAction extends BaseAction{

    @Autowired
    private MachineRepository machineRepository;

    @Autowired
    private WeixinRepository weixinRepository;

    @Autowired
    private UserRepository userRepository;

    @RequestMapping("/machineAll")
    public ResultVO machineAll(){

        return success(machineRepository.findAll());
    }

    @RequestMapping("/searchMachine")
    public ResultVO searchMachine(@RequestParam(value = "page", defaultValue = "1") int page, @RequestParam(value = "size", defaultValue = "5") int size, String param, Integer type){

        Subject subject=SecurityUtils.getSubject();
        page = page<0?0:page;
        Sort sort = new Sort(Sort.Direction.DESC, "id");
        Pageable pageable = new PageRequest(page-1, size, sort);
        if(type==1){
            return success(machineRepository.selectPageByImei(param,pageable));
        }
        return success(machineRepository.selectPageByAccount(param,pageable));
    }

    @RequestMapping("/findEditMachie")
    public ResultVO findEditMachie(int machineId){

        return success(machineRepository.findEditMachie(machineId));
    }

    @RequestMapping("/save")
    public ResultVO save(Machine machine){

//        User user= (User) SecurityUtils.getSubject().getSession().getAttribute("user");
        User user= (User) SecurityUtils.getSubject().getPrincipal();
        Machine oldMachine=machineRepository.getOne(machine.getId());
        if(user.getRoleId()!=1){
            if(oldMachine.getStatus()!=Machine.STATUS_ACTIVE&&machine.getStatus()==Machine.STATUS_ACTIVE){
                if(oldMachine.getStatus()!=Machine.STATUS_FROZEN){
                    return error("只有冻结设备才能激活");
                }
            }
            if(oldMachine.getStatus()!=machine.getStatus()){
                Integer[] statusArray={Machine.STATUS_NEW,Machine.STATUS_SELL,Machine.STATUS_REMOVE,Machine.STATUS_RETURN,Machine.STATUS_RETURN_CHANGE_IMEI,Machine.STATUS_FACTORY_RECOVERY};
                if(user.getRoleId()!=1&& ArrayUtils.contains(statusArray, machine.getStatus())){
                    return error("该用户没有权限修改设备为此状态");
                }
            }
        }
        oldMachine.setGroupType(machine.getGroupType());
        oldMachine.setUserStatus(machine.getUserStatus());
        oldMachine.setStatus(machine.getStatus());
        oldMachine.setProductType(machine.getProductType());
        machineRepository.save(oldMachine);
        return success("保存成功");
    }

    @RequestMapping("/untying")
    @Transactional
    public ResultVO untying(Long id){

        Machine machine=machineRepository.getOne(id);
        if (machine == null)
            return error("设备不存在");
        if (machine.getStatus() != Machine.STATUS_ACTIVE) {
            return error("不是激活设备不需要解绑!"+machine.getStatus());
        }
        Integer userId = machine.getUserId();
        if (userId != null) {
            User user = userRepository.getOne(machine.getUserId().longValue());
            if (user == null)
                return error("设备绑定用户不存在！");
            // 减少用户设备数量
            user.setMachineCount(user.getMachineCount() - 1);
            userRepository.save(user);
        }
        machine.setUserId(null);
        machine.setStatus(Machine.STATUS_SELL);
        machine.setSellAgentId(null);
        machine.setPurchaser(null);
        machine.setActivationTime(null);
        machineRepository.save(machine);
        weixinRepository.deleteByMachineId(machine.getId().intValue());
        return success("解绑成功");
    }
}
